/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function Lidas()
{
    this.regex = {};
    this.cache = {};
    this.dump = function (variable){
        var ret = "";
        for(var i in variable)
            try{
                ret += i + "=" + (typeof(variable[i]) == "function" )?"function":
                variable[i] + "\n";
            } catch (ex){
                ret += i + "=[Error]"+ex;
            }
        return ret;
    }

    this.log =function(msg){
        $("#log").html($("#log").html()+"\n"+msg);
    }

    this.get = function(refname){
        var ret = lidas;
        $.each(refname.split('_'),function(){
            ret=ret[this];
        })
        return ret;
    }

    this.finddata = function($key){
        var $this = $(this);
        var $value = $this.data($key);
        if($value==null){
            if($this.parentNode == document)
                return null;
            $value = lidas.finddata.call($this.parentNode,$key);
            $this.data($key,$value);
        }
        return $value;
    }

    this.check = function(event){
        var $ret = true;
        var $this = $(this).closest('[data-type]')[0];
        if($this){
            $this = $($this);
            var $type = $this.data('type');
            var $found = false;
            if($type!=null){
                $.each($type.split(' '),function(){
                    var $class = lidas.get(this);
                    var $method = $class[event.type];
                    if(($method!=null)&&(typeof($method) == "function")){
                        $ret &= $method.call($this,event);
                        $found = true;
                    }
                })
            }
            if(!$found)
                return lidas.check.call($this.parent(),event);            
        }
        return $ret;
    }

    this.user = {};
    this.user.click = function(event){
        lidas.log(this.html());
        return false;
    }

    this.numeric = {};
    this.numeric.keyup = function(event){
        var orig = this.attr("value");
        var novo = orig.replace(/\D/g,"");
        if(orig != novo)
            this.attr("value",novo);
        return true;
    }

    this.drag = {};
    this.drag.mousedown = function(event){
    }

    this.size = {};
    this.size.keydown = function(event){
        this.attr("prev",this.attr("value"));
        return true;
    }
    this.size.keyup = function(event){
        var limit = this.attr("maxlength") || this.attr("size");
        if( (this.attr("prev") != this.attr("value")) &&
            (this.attr("value").length >= limit))
            $(":input:eq("+($(":input").index(this)+1)+")").focus();
        return true;
    }

    this.send = function (){
        var $this = $(this);
        $.ajax({
            type: 'post',
            url: 'json.php',
            data: {
                request: JSON.stringify($this.data())
            },
            context: this
        }).done(function(data) {
            try{
                var jdata = JSON.parse(data);
                if(jdata.exception)
                    throw new Error(jdata.exception);
                var fn = lidas.get($this.data('type'));
                if(typeof(fn.JSON)=="function"){
                    fn.JSON.call($this,jdata);
                }
            } catch (ex) {
                lidas.log(ex.stack);
            }
        });
    }
    this.tick = function () {
        $('.lazy[data-id]:lt(3)').removeClass('lazy').each(lidas.send);
        setTimeout(lidas.tick, 500);
    }
    setTimeout(this.tick, 100);

    this.sortable = {};
    this.sortable.click = function(event){
        return false;
    }

    $(document).on('focus',"[contenteditable]",function(event) {
        var $this = $(this);
        $this.data('before', $this.html());
        return true;
    });
    $(document).on('blur',"[contenteditable]", function(event) {
        var $this = $(this);
        if ($this.data('before') !== $this.html()) {
            $this.data('before', $this.html());
            $this.data('id',$this.parent().data('id'));
            $this.data('values',$this.html().split('<hr/>'));
            lidas.send.call($this);
        }
        return true;
    });
    $(document).bind("click dblclick change keydown keyup mousedown mouseup contextmenu change",function (event){
        try{
            return lidas.check.call(event.target, event);
        } catch (ex) {
            lidas.log(ex.toString() + ex.stack);
        }
        return false;
    });
    return this;
}
lidas=Lidas();


